<!--#
layout("/layouts/platform.html"){
#-->

<header class="header navbar bg-white shadow">
    <div class="btn-group tool-button">
        <a class="btn btn-primary navbar-btn" href="${base}/platform/sys/api/add" data-toggle="modal" data-target="#dialogDetail"><i class="ti-plus"></i> ${msg['globals.button.add']}</a>
    </div>
</header>
<div class=panel-body style="padding-top: 50px;">
    <div class="table-responsive no-border" style="min-height: 500px">
        <input id="unitid" type="hidden">
        <table class="table table-bordered table-striped mg-t datatable">
            <thead>
            <tr>
                <th>${msg['sys.api.column.appName']}</th>
                <th>${msg['sys.api.column.appId']}</th>
                <th>${msg['sys.api.column.appKey']}</th>
                <th>${msg['sys.api.column.disabled']}</th>
                <th>${msg['sys.conf.operation']}</th>
        </table>
    </div>
</div>
<div id="dialogDelete" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">${msg['sys.conf.del']}</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        ${msg['sys.conf.del.note']} <br/>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">${msg['globals.button.cancel']}</button>
                <button id="ok" type="button" class="btn btn-primary" data-loading-text="${msg['globals.button.delete.tip']}">${msg['globals.button.confirm']}</button>
            </div>
        </div>
    </div>
</div>
<div id="dialogReset" class="modal fade bs-modal-sm" tabindex="-2" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">${msg['sys.api.tip.reset']}</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        ${msg['sys.api.tip.note']}
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">${msg['globals.button.cancel']}</button>
                <button id="okReset" type="button" class="btn btn-primary" data-loading-text="${msg['globals.button.delete.tip']}">${msg['globals.button.confirm']}</button>
            </div>
        </div>
    </div>
</div>
<div id="dialogDetail" class="modal fade bs-modal" tabindex="-3" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

        </div>
    </div>
</div>
<script language="JavaScript">
    var datatable;
    function initDatatable() {
        datatable = $('.datatable').DataTable({
            "dom": '<"toolbar">frtip',
            "searching": false,
            "processing": false,
            "serverSide": true,
            "select": true,
            "ordering": true,
            "language": {
                "url": "${base}/assets/platform/vendor/datatables/locale/${lang!}.json"
            },
            "preDrawCallback": function () {
                sublime.showLoadingbar($(".main-content"));
            },
            "drawCallback": function () {
                sublime.closeLoadingbar($(".main-content"));
            },
            "ajax": {
                "url": "${base}/platform/sys/api/data",
                "type": "post",
                "data": function (d) {
                }
            },
            "order": [[0, "asc"]],
            "columns": [
                {"data": "appName", "bSortable": true},
                {"data": "appId", "bSortable": true},
                {"data": "appKey", "bSortable": true},
                {"data": "disabled", "bSortable": true}
            ],
            "columnDefs": [
                {
                    "render": function (data, type, row) {
                        if (!data) {
                            return '<i id="disabled_' + row.id + '" class="fa fa-circle text-success ml5"></i>';
                        } else {
                            return '<i id="disabled_' + row.id + '" class="fa fa-circle text-danger ml5"></i>';
                        }
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row) {
                        return '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' +
                                ' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' +
                                '<li><a href="javascript:;" onclick="resetIt(\'' + row.id + '\')">${msg["globals.button.reset"]}</a></li>' +
                                '<li><a href="javascript:;" onclick="del(\'' + row.id + '\')">${msg["globals.button.delete"]}</a></li>' +
                                '<li><a href="javascript:;" onclick="enable(\'' + row.id + '\')">${msg["globals.button.enable"]}</a></li>' +
                                '<li><a href="javascript:;" onclick="disable(\'' + row.id + '\')">${msg["globals.button.disable"]}</a></li>' +
                                '</ul></div>';
                    },
                    "targets": 4
                }
            ]
        });
        datatable.on('click', 'tr', function () {
            $(this).toggleClass('selected');
        });
        $("#searchBtn").on('click', function () {
            datatable.ajax.reload();
        });
    }
    function resetIt(id) {
        var dialog = $("#dialogReset");
        dialog.modal("show");
        dialog.find("#okReset").unbind("click");
        dialog.find("#okReset").bind("click", function (event) {
            var btn = $(this);
            btn.button("loading");
            $.post("${base}/platform/sys/api/reset/" + id, {}, function (data) {
                if (data.code == 0) {
                    datatable.ajax.reload(null, false);
                } else {
                    Toast.error(data.msg);
                }
                //重置按钮状态，关闭提示框
                btn.button("reset");
                dialog.modal("hide");
            }, "json");
        });
    }
    function del(id) {
        var dialog = $("#dialogDelete");
        dialog.modal("show");
        dialog.find("#ok").unbind("click");
        dialog.find("#ok").bind("click", function (event) {
            var btn = $(this);
            btn.button("loading");
            $.post("${base}/platform/sys/api/delete/" + id, {}, function (data) {
                if (data.code == 0) {
                    datatable.ajax.reload(null, false);
                } else {
                    Toast.error(data.msg);
                }
                //重置按钮状态，关闭提示框
                btn.button("reset");
                dialog.modal("hide");
            }, "json");
        });
    }
    function enable(id) {
        $.post("${base}/platform/sys/api/enable/" + id, {}, function (data) {
            if (data.code == 0) {
                $("#disabled_"+id).attr("class", "fa fa-circle text-success ml5");
                Toast.success(data.msg);
            } else {
                Toast.error(data.msg);
            }
            btn.button("reset");
        }, "json");
    }

    function disable(id) {
        $.post("${base}/platform/sys/api/disable/" + id, {}, function (data) {
            if (data.code == 0) {
                $("#disabled_"+id).attr("class", "fa fa-circle text-danger ml5");
                Toast.success(data.msg);
            } else {
                Toast.error(data.msg);
            }
            btn.button("reset");
        }, "json");
    }
    $(function () {
        initDatatable();
    });
</script>

<!--#}#-->